#!/bin/bash
#
#Wait until a specified user logs on
#
#Set up default values
mailopt=FALSE
interval=60
#process command options
while getopt mt: option
do
	case "$option"
	    in
		m) mailopt=TRUE;;
		t) interval=$OPTARG;;
		\?) echo "Usage: mon [-m] [-t n] user"
		    echo "   -m means to be informed by mail"
		    echo "   -t means check every n secs."
		    EXIT 1;;
	esac
done

#Make sure a user name was specified
if [ "$OPTIND" -gt "$#" ]
then
	echo "Missing user name!"
	exit 2
fi

shiftcount=$((OPTIND - 1))
shift $shiftcount
user=$1

#
#Check for user logging on
#

until who | grep "^$user " > /dev/null
do
	sleep $interval
done

#
#When we reach this point, the user has logged on
#
if [ "$mailopt" = FALSE ]
then 
	echo "$user has logged on"
else
	runner=$(who am I | cut -cl -8)
	echo "$user has logged on" | mail $runner
fi
